package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.CcLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired//从spring容器中拿出来
    UserService userService;

    @PostMapping("/add")
    @CcLogs(operation = "用户信息",type = LogType.ADD)
    public Result add(@RequestBody User user) {
        try {
            userService.save(user);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    //    修改用户信息
    @PutMapping("/update")
    @CcLogs(operation = "用户信息",type = LogType.UPDATE)
    public Result update(@RequestBody User user) {
        userService.updateById(user);
        return Result.success();
    }

    //    删除用户信息
    @DeleteMapping("/delete/{id}")
    @CcLogs(operation = "用户信息",type = LogType.DELETE)
    public Result delete(@PathVariable Integer id) {
        userService.removeById(id);
        return Result.success();
    }

    //    批量删除
    @DeleteMapping("/delete/double")
    @CcLogs(operation = "用户信息",type = LogType.DOUBLEDELETE)
    public Result doubleDelete(@RequestBody List<Integer> ids) {
        userService.removeBatchByIds(ids);
        return Result.success();
    }

    //    查询全部
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<User> userList = userService.list(new QueryWrapper<User>().orderByDesc("id"));//select * from user order by id desc
        return Result.success(userList);
    }

    //    id查询
    @GetMapping("/selectById/{id}")
    public Result selectId(@PathVariable Integer id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    //   username查询
    @GetMapping("/selectByUserName")
    public Result checkUsernameUnique(@RequestParam String username) {
        User user = userService.getByUsername(username);
        if (user != null) {
            return Result.error("用户名已存在");
        } else {
            return Result.success();
        }
    }

    //    分页多条件模糊查询
    //    pageNum当前页码
    //    一页多少个
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam String name,
                               @RequestParam String username,
                               @RequestParam Integer pageNum,
                               @RequestParam Integer pageSize) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(username), "username", username);
        queryWrapper.like(StrUtil.isNotBlank(name), "name", name);
        // 上两条等同于select * from user where username like '%#{username}%' and name like '%#{name}#%'
        Page<User> page = userService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
}

